var util = require('../../utils/util')
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    report: {},

    //审核结果 0=不通过1=通过
    auditResult: 1,

    //审核备注
    auditRemark: '同意',

    //审批流程记录
    reportRecord: []


  },

  bindauditRemark(event) {
    this.data.auditRemark = event.detail.value

  },
  switchChange(event) {
    if (event.detail.value) {
      this.data.auditResult = 1
      this.setData({
        auditRemark: '同意'
      })
    } else {
      this.data.auditResult = 0
      this.setData({
        auditRemark: '不同意'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('row', function (data) {
      console.log(data)
      that.setData({
        report: data,
        imgList: (data.fliePath || '').split(';').filter(item => item !== ''),
        imgListed: (data.verifyImage || '').split(';').filter(item => item !== '')
      })
      //获取历史审批记录
      that.getRecord(data.reportId)

    })
    

  },


  /**
   *获取历史审批记录
   */
  getRecord(reportId) {
    let that = this
    var pageInfo = {
      reportId:reportId
    }
    that.loadModal();
    util.request(api.getReportRecord, JSON.stringify(pageInfo), 'POST').then(function (res) {
      console.log(res)
      if (res.code === "200") {
        that.closeModal();
        //刷新界面
        that.setData({
          reportRecord: res.data,
        });
      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });
  },


  //浏览图片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  //浏览核实照片
  ViewImageed(e) {
    wx.previewImage({
      urls: this.data.imgListed,
      current: e.currentTarget.dataset.url
    });
  },

  checkPhone(event) {
    let row = event.currentTarget.dataset.data;
    wx.makePhoneCall({
      phoneNumber: row,
      success: function (res) {
        console.log(res)
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },


  //进度条
  loadModal() {
    this.setData({
      loadModal: true
    })
  },
  closeModal() {
    this.setData({
      loadModal: false
    })
  },
  //保存提交
  save() {
    const userInfoDto1 = wx.getStorageSync('userInfoDto')
    if (this.data.auditRemark === '' || this.data.auditRemark == null) {
      util.showErrorToast('请输入审批意见！');
      return false
    }

    //提交上报数据
    let dataup = {
      reportId: this.data.report.reportId,
      auditResult: this.data.auditResult,
      auditRemark: this.data.auditRemark
    };
    let that = this;
    that.loadModal();
    let url=''
    if (userInfoDto1.roleCodeList.includes('role_supervise')) {
      url=api.superviseAudit
    }
    if (userInfoDto1.roleCodeList.includes('role_biz')) {
      url=api.bizAudit
    }
    if (userInfoDto1.roleCodeList.includes('role_safety')) {
      url=api.safetyAudit
    }
    if (userInfoDto1.roleCodeList.includes('role_charge_leader')) {
      url=api.chargeAudit
    }
    if (userInfoDto1.roleCodeList.includes('role_main_leader')) {
      url=api.mainAudit
    }
 
    util.request(url, JSON.stringify(dataup), 'POST').then(function (res) {
      console.log(res)
      if (res.code == '200') {
        that.closeModal();
        //刷新界面
        console.log("========")
        wx.navigateBack()

      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });


  },


})